﻿using ACC.Business.Stn;
using ACC.Business;
using ACC.Data;
using ACC.Data.Comm;
using ACC.Data.Line;
using ACC.Device;
using System;
using ACC.Data.Entities.Mes;
using System.Collections.Generic;
using ACC.LineDevice.KukaPack;
using ACC.Data.ACCLog;
using System.Linq;
using ACC.Device.PLC.Siemens;
using System.Text;

namespace Script
{
    /// <summary>
    /// 电芯涂胶
    /// OP L1M02070, STATION L1M12071, L1M12072, L1M22071, L1M22071
    /// </summary>
    class L1M02070 : StnProcessPL02_DX
    { 
        public L1M02070(StnRunningData data)
            : base(data)
        {
            IsReport = false;
            IsACCReport = false;
            IsPalletRelationSN = false;
        }

         protected override ACCDevice CreateDevice()
         {
             ACCDevice dev = base.CreateDevice();
             if (kukaSiemensDev != null)
             {
                 switch (_data.StnCfg.Name)
                 {
                     case "L1M12071":
                     case "L1M22071":
                         {
                             kukaSiemensDev.DB190_Offset = 8000;
                         }
                         break;
                     case "L1M12072":
                     case "L1M22072":
                         {
                             kukaSiemensDev.DB190_Offset = 6000;
                         }
                         break;
                 }

                 kukaSiemensDev.DB190 = 800;
                 //kukaSiemensDev.DB190_Offset = 8000;
                 kukaSiemensDev.DB192 = 80;
                 kukaSiemensDev.DB192_PLC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 290);
                 kukaSiemensDev.DB193 = 800;
                 kukaSiemensDev.DB193_PLC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 66);
                 kukaSiemensDev.DB193_ACC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 68);
                 kukaSiemensDev.DB194 = 800;
                 kukaSiemensDev.DB194_PLC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 66);
                 kukaSiemensDev.DB194_PLC_Data_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 302);

                 kukaSiemensDev.BarcodeLength = 30;
                 kukaSiemensDev.LoadBarcodeCount = 6;
                 kukaSiemensDev.UnloadBarcodeCount = 6;
                 kukaSiemensDev.ProcessDataStringDataLength = 8;
                 kukaSiemensDev.ProcessDataStringLength = 6;
                 kukaSiemensDev.ProcessDataRealLength = 60;

                 kukaSiemensDev.Material_COMM_Offset = 292;       // 上下料信号位偏移量
                 kukaSiemensDev.PLC_MaterialData_Offset = 700;
             }

             return dev;

         }

         protected override void CreateBusinessLogic()
         {
             base.CreateBusinessLogic();

             (_loadProcess as LoadProcess_DX).R = 10;
             (_unloadProcess as UnloadProcess_DX).R = 10;
             (_unloadProcess as UnloadProcess_DX).S = 1;
             (_unloadProcess as UnloadProcess_DX).OneAsNG = true;
         }

         public override string GetMaterialBarcode(int index)
         {
             byte[] data = new byte[64];
             byte[] temp = new byte[62];
             ((SiemensDevice)_Device).ReadDBBytes(((SiemensDevice)_Device).DB193, kukaSiemensDev.DB190_Offset + kukaSiemensDev.PLC_MaterialData_Offset + (index * 66), data);
             Array.Copy(data, 2, temp, 0, temp.Length);
             string _materialBarcode = Encoding.ASCII.GetString(temp);
             if (_materialBarcode != null && _materialBarcode.Length > data[1])
             {
                 _materialBarcode = _materialBarcode.Substring(0, data[1]);
             }
             return _materialBarcode;
         }

        /// <summary>
        /// 从内存中移除（避免后面主线出现问题）
        /// </summary>
        /// <param name="dev"></param>
        /// <param name="result"></param>
        /// <returns></returns>
         public override ACCCode SendUnloadResult(ACCDevice dev, ACCDevice.UnloadResult result)
         {
             ACCCode rs = base.SendUnloadResult(dev, result);
             if(rs == ACCCode.ACCOK && result.ErrorCode == ACCCode.ACCOK)
             {
                 ACC.Business.Process.UnitDataProcess.RemoveWIPUnitFromLineCache(_lineData.RunningData, _data.LastUnloadUnit);
             }

             return rs;
         }
    }
}
